package Week7.J0527;

import java.util.Scanner;

public class 滑雪_xv {
    // dfs
    static int[] dx = {0, 0, 1, -1};
    static int[] dy = {1, -1, 0, 0};
    static int n, m;
    static int[][] arr;
    static int[][] dp;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); m = scanner.nextInt();
        arr = new int[n][m];
        dp = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }

        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ret = Math.max(ret, dfs(i, j));
            }
        }
        System.out.println(ret);
    }

    private static int dfs(int i, int j) {
        if (dp[i][j] != 0) return dp[i][j];
        int len = 1;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] < arr[i][j]) {
                len = Math.max(len, 1 + dfs(x, y));
            }
        }
        dp[i][j] = len;
        return len;
    }
}
